home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / Found / FWMemory / Sources / FWMemMgr.cpp < prev    next >
Encoding:
Text File  |  1995-11-08  |  14.8 KB  |  514 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWMemMgr.cpp
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Copyright:    (c) 1993, 1995 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWFound.hpp"
  11.  
  12. #ifndef   FWMEMMGR_H
  13. #include "FWMemMgr.h"
  14. #endif
  15.  
  16. #ifndef FWEXCDEF_H
  17. #include "FWExcDef.h"
  18. #endif
  19.  
  20. #ifndef   FWMEMTAS_H
  21. #include "FWMemTas.h"
  22. #endif
  23.  
  24. #ifndef FWMEMHLP_H
  25. #include "FWMemHlp.h"
  26. #endif
  27.  
  28. #ifndef FWNEW_H
  29. #include "FWNew.h"
  30. #endif
  31.  
  32. #if defined(FW_BUILD_MAC) && !defined(__OSUTILS__)
  33. #include <OSUtils.h>
  34. #endif
  35.  
  36. #if defined(FW_BUILD_MAC) && !defined(__MEMORY__)
  37. #include <Memory.h>
  38. #endif
  39.  
  40. #if defined(FW_BUILD_WIN) && !defined(_INC_WINDOWS)
  41. #include <Windows.h>
  42. #endif
  43.  
  44. #if defined(FW_BUILD_WIN) && !defined(_INC_WINDOWSX)
  45. #include <WindowsX.h>
  46. #endif
  47.  
  48. #if !defined(FW_qUsePlatformAlloc) && !defined(FW_qUseCRuntimeAlloc)
  49.  
  50. #ifdef FW_BUILD_WIN
  51. #include <MMStubs.h>
  52. #endif
  53.  
  54. #ifdef FW_BUILD_MAC
  55. #include <MemMgr.h>
  56. #endif
  57.  
  58. #endif
  59.  
  60. #include <string.h>
  61.  
  62. #if defined(__SC__) && defined(FW_BUILD_WIN)
  63.  
  64. #if !defined(__LIMITS_H)
  65. #include <limits.h>
  66. #endif
  67.  
  68. #if !defined(__HUGEPTR_H) && defined(FW_BUILD_WIN16)
  69. #include <hugeptr.h>
  70. #endif
  71.  
  72. #endif
  73.  
  74. #if FW_LIB_EXPORT_PRAGMAS
  75. #pragma lib_export on
  76. #endif
  77.  
  78. #define PRIV_CLEAR_MEM_ON_ALLOC
  79.  
  80. // It'd be cool to let this be set at program startup using InitializationFile setting
  81. #ifdef PRIV_CLEAR_MEM_ON_ALLOC
  82. const unsigned char kKnownRawByte = 0;
  83. #else
  84. const unsigned char kKnownRawByte = 0x5A;
  85. #endif
  86.  
  87. #ifdef FW_BUILD_MAC
  88. #pragma segment FWMemory
  89. #endif
  90.  
  91. //========================================================================================
  92. // Global procedures
  93. //========================================================================================
  94.  
  95. #ifdef FW_BUILD_MAC
  96. //----------------------------------------------------------------------------------------
  97. // SetStackSpace
  98. //----------------------------------------------------------------------------------------
  99.  
  100. void SetStackSpace(long numBytes)
  101. {
  102.     long newLimit;
  103.  
  104.     // Make sure numBytes is even
  105.     if (numBytes % 2) numBytes++;
  106.  
  107.     newLimit = (long)LMGetCurStackBase() - numBytes;
  108.     if ((long)GetApplLimit() > newLimit)
  109.         SetApplLimit((Ptr)newLimit);
  110. } // SetStackSpace 
  111. #endif
  112.  
  113.  
  114. #if defined(FW_BUILD_WIN) && defined(FW_qUsePlatformAlloc)
  115. //----------------------------------------------------------------------------------------
  116. // GetPointer
  117. //----------------------------------------------------------------------------------------
  118.  
  119. static void*  GetPointer(HANDLE h)
  120. {
  121.     return (void*)::GlobalLock(h);
  122. }
  123. #endif
  124.  
  125. #if defined(FW_BUILD_WIN) && defined(FW_qUsePlatformAlloc)
  126. //----------------------------------------------------------------------------------------
  127. // GetHandle
  128. //----------------------------------------------------------------------------------------
  129.  
  130. static HANDLE GetHandle(void* p)
  131. {
  132.     return p != NULL ? GlobalPtrHandle(p) : NULL;
  133. }
  134. #endif
  135.  
  136. #ifdef FW_SET_NEW_HANDLER
  137. //----------------------------------------------------------------------------------------
  138. // set_new_handler
  139. //----------------------------------------------------------------------------------------
  140.  
  141. FW_PFVV set_new_handler(FW_PFVV handler)
  142. {
  143.     FW_SPrivMemoryGlobals& globals = FW_CMemoryTaskGlobals::GetMemoryGlobals();
  144.     FW_PFVV oldHandler = globals.gNewHandler;
  145.     globals.gNewHandler = handler;
  146.     return oldHandler;
  147. }
  148. #endif
  149.  
  150. //========================================================================================
  151. // CLASS FW_CMemoryManager
  152. //========================================================================================
  153.  
  154. //----------------------------------------------------------------------------------------
  155. // FW_CMemoryManager::GetMemoryHeap
  156. //----------------------------------------------------------------------------------------
  157.  
  158. MemHeap* FW_CMemoryManager::GetMemoryHeap()
  159. {
  160.     MemHeap* heap = FW_CMemoryTaskGlobals::GetMemoryGlobals().gMemoryHeap;
  161.     FW_ASSERT(heap != NULL);
  162.     
  163.     return heap;
  164. }
  165.  
  166. //----------------------------------------------------------------------------------------
  167. // FW_CMemoryManager::DefaultNewHandler
  168. //----------------------------------------------------------------------------------------
  169.  
  170. void FW_CMemoryManager::DefaultNewHandler()
  171. {
  172.     FW_Failure(FW_xMemoryExhausted);
  173. }
  174.  
  175. //----------------------------------------------------------------------------------------
  176. // FW_CMemoryManager::AddOffsetToPointer
  177. //----------------------------------------------------------------------------------------
  178.  
  179. void *FW_CMemoryManager::AddOffsetToPointer(void *pointer, unsigned long Offset)
  180. {
  181. #if defined(FW_BUILD_MAC) || defined(FW_BUILD_WIN32)
  182.     return (void *)((char *)pointer + Offset);
  183. #elif defined(__SC__) && defined(FW_BUILD_WIN16)
  184.     // If 'Offset' is not less than the maximum signed LONG Value, consider it an fError.
  185.     // We could program for these cases, but it is More than likely an fError.
  186.     FW_ASSERT(Offset < (unsigned long)LONG_MAX);
  187.     
  188.     // Symantec C++ note: hugeptr_add(p,o) is a macro that calls the function:
  189.     //        hugeptr_add(p, sizeof(*p) * o)
  190.     // Thus, the pointer passed to hugeptr_add() must be 'char *' type.
  191.     char * typedPointer                = pointer;
  192.     signed long signedLongOffset    = Offset;
  193.     
  194.     // The call to hugeptr_add() is not scoped with '::' because Symantec C++
  195.     // first uses a macro and the macro fails to compile when scoped with '::'.
  196.     return hugeptr_add(typedPointer, signedLongOffset);
  197. #else
  198.     return (char __huge* pointer) + Offset;
  199. #endif
  200. }
  201.  
  202. //----------------------------------------------------------------------------------------
  203. // FW_CMemoryManager::CopyMemory - 
  204. //----------------------------------------------------------------------------------------
  205. void FW_CMemoryManager::CopyMemory(const void* const source,
  206.                                    void* const destination,
  207.                                    unsigned long bytesToMove)
  208. {
  209.     FW_ASSERT(source != 0);
  210.     FW_ASSERT(destination != 0);
  211.  
  212. #if defined(FW_BUILD_MAC)
  213.     ::BlockMoveData(source, destination, bytesToMove);
  214. #elif defined(FW_BUILD_WIN)
  215.     ::FW_PrimitiveCopyMemory(source, destination, bytesToMove);
  216. #endif
  217. }
  218.  
  219. //----------------------------------------------------------------------------------------
  220. // FW_CMemoryManager::SetMemory - 
  221. //----------------------------------------------------------------------------------------
  222. void FW_CMemoryManager::SetMemory(void *aBlock,
  223.                                   unsigned long bytesToSet,
  224.                                   unsigned char byteValue)
  225. {
  226.     FW_ASSERT(aBlock != 0);
  227.  
  228.     register char *cp;
  229.     cp = (char *)aBlock;
  230.     while (bytesToSet--)
  231.         *cp++ = byteValue;
  232. }
  233.  
  234. //----------------------------------------------------------------------------------------
  235. // InitializeRawBlock - Set a raw block of fMemory to known values.
  236. //----------------------------------------------------------------------------------------
  237. void FW_CMemoryManager::InitializeRawBlock(void *aBlock, unsigned long sizeBlock)
  238. {
  239. #ifdef FW_DEBUG
  240.     SetMemory(aBlock, sizeBlock, kKnownRawByte);
  241. #endif
  242. }
  243.  
  244. //----------------------------------------------------------------------------------------
  245. // FW_CMemoryManager::AllocateBlock - Allocate a non-relocatable block of fMemory.
  246. //----------------------------------------------------------------------------------------
  247. void* FW_CMemoryManager::AllocateBlock(unsigned long bytesRequested)
  248. {
  249.     unsigned long bytesNeeded = bytesRequested;
  250.     void* aBlock = FW_PrimitiveAllocateBlock(bytesNeeded);
  251.     if (aBlock == 0)
  252.         FW_Failure(FW_xMemoryExhausted);
  253.  
  254.     InitializeRawBlock(aBlock, bytesNeeded);
  255.  
  256.     return aBlock;
  257. }
  258.  
  259. #ifdef FW_BUILD_MAC
  260. #pragma segment FWMemMgr2
  261. #endif
  262.  
  263. //----------------------------------------------------------------------------------------
  264. // FW_CMemoryManager::ResizeBlock - Resize a non-relocatable block of fMemory.  The
  265. //    block may be moved to a new location if necessary.
  266. //----------------------------------------------------------------------------------------
  267. void *FW_CMemoryManager::ResizeBlock(void *aBlock, unsigned long bytesRequested)
  268. {
  269.     unsigned long bytesNeeded = bytesRequested;
  270.     
  271.     void *newBlock;
  272.     if (aBlock == 0)
  273.         newBlock = FW_PrimitiveAllocateBlock(bytesNeeded);
  274.     else
  275.         newBlock = FW_PrimitiveResizeBlock(aBlock, bytesNeeded);
  276.  
  277.     if (newBlock == 0)
  278.         FW_Failure(FW_xMemoryExhausted);
  279.     
  280.     return newBlock;
  281. }
  282.  
  283. //----------------------------------------------------------------------------------------
  284. // FW_CMemoryManager::FreeBlock - Return a non-relocatable block to the free store.
  285. //----------------------------------------------------------------------------------------
  286. void FW_CMemoryManager::FreeBlock(void *aBlock)
  287. {
  288.     if (aBlock != 0)
  289.         FW_PrimitiveFreeBlock(aBlock);
  290. }
  291.  
  292. //----------------------------------------------------------------------------------------
  293. // FW_CMemoryManager::AllocateSystemHandle - 
  294. //----------------------------------------------------------------------------------------
  295. FW_PlatformHandle FW_CMemoryManager::AllocateSystemHandle(unsigned long bytesNeeded)
  296. {
  297.     FW_PlatformHandle aSystemHandle;
  298.  
  299. #if !defined FW_qUsePlatformAlloc
  300.     aSystemHandle = (FW_PlatformHandle) ::MMAllocateHandle(bytesNeeded);
  301. #elif defined FW_BUILD_MAC
  302.     aSystemHandle = ::NewHandle(bytesNeeded);
  303. #elif defined FW_BUILD_WIN
  304.     aSystemHandle = ::GlobalAlloc(GMEM_MOVEABLE, bytesNeeded);
  305. #endif
  306.     
  307.     if (aSystemHandle == 0)
  308.         FW_Failure(FW_xMemoryExhausted);
  309.  
  310.     return aSystemHandle;
  311. }
  312.  
  313. #ifdef FW_BUILD_MAC
  314. #pragma segment FWMemMgr3
  315. #endif
  316.  
  317. //----------------------------------------------------------------------------------------
  318. // FW_CMemoryManager::ResizeSystemHandle - 
  319. //----------------------------------------------------------------------------------------
  320. FW_PlatformHandle FW_CMemoryManager::ResizeSystemHandle(FW_PlatformHandle aHandle,
  321.                                                         unsigned long bytesNeeded)
  322. {
  323.     FW_ASSERT(aHandle != 0);
  324.  
  325.     FW_PlatformHandle newHandle = aHandle;
  326.  
  327. #if !defined FW_qUsePlatformAlloc
  328.     ::MMSetHandleSize(aHandle, bytesNeeded);
  329. #elif defined FW_BUILD_MAC
  330.     ::SetHandleSize(aHandle, bytesNeeded);
  331. #elif defined FW_BUILD_WIN
  332.     newHandle = ::GlobalReAlloc(aHandle, bytesNeeded, GMEM_MOVEABLE);
  333. #endif
  334.     
  335.     if (newHandle == 0 || GetSystemHandleSize(newHandle) < bytesNeeded)
  336.         FW_Failure(FW_xMemoryExhausted);
  337.  
  338.     return newHandle;
  339. }
  340.  
  341. #ifdef FW_BUILD_MAC
  342. #pragma segment FWMemMgr2
  343. #endif
  344.  
  345. //----------------------------------------------------------------------------------------
  346. // FW_CMemoryManager::FreeSystemHandle - 
  347. //----------------------------------------------------------------------------------------
  348. void FW_CMemoryManager::FreeSystemHandle(FW_PlatformHandle aHandle)
  349. {
  350. #if !defined FW_qUsePlatformAlloc
  351.     if (aHandle)
  352.         ::MMFreeHandle(aHandle);
  353. #elif defined FW_BUILD_MAC
  354.     if (aHandle)
  355.         ::DisposeHandle(aHandle);
  356. #elif defined FW_BUILD_WIN
  357.     if (aHandle)
  358.         ::GlobalFree(aHandle);
  359. #endif
  360. }
  361.  
  362. #ifdef FW_BUILD_MAC
  363. #pragma segment FWMemMgr4
  364. #endif
  365.  
  366. //----------------------------------------------------------------------------------------
  367. // FW_CMemoryManager::LockSystemHandle - 
  368. //----------------------------------------------------------------------------------------
  369. void * FW_CMemoryManager::LockSystemHandle(FW_PlatformHandle handle)
  370. {
  371.     FW_ASSERT(handle != 0);
  372.  
  373.     void* memory;
  374.     
  375. #if !defined FW_qUsePlatformAlloc
  376.     memory = ::MMLockHandle(handle);
  377. #elif defined FW_BUILD_MAC
  378.     HLock(handle);
  379.     memory = *handle;
  380. #elif defined FW_BUILD_WIN
  381.     memory = ::GlobalLock(handle);
  382. #endif
  383.  
  384.     if(memory == NULL)
  385.         FW_Failure(FW_xMemoryExhausted);
  386.         
  387.     return memory;
  388. }
  389.  
  390. #ifdef FW_BUILD_MAC
  391. #pragma segment FWMemMgr5
  392. #endif
  393.  
  394. //----------------------------------------------------------------------------------------
  395. // FW_CMemoryManager::UnlockSystemHandle - 
  396. //----------------------------------------------------------------------------------------
  397. void FW_CMemoryManager::UnlockSystemHandle(FW_PlatformHandle aHandle)
  398. {
  399.     FW_ASSERT(aHandle != 0);
  400.     
  401. #if !defined FW_qUsePlatformAlloc
  402.     ::MMUnlockHandle(aHandle);
  403. #elif defined FW_BUILD_MAC
  404.     ::HUnlock(aHandle);
  405. #elif defined FW_BUILD_WIN
  406.     ::GlobalUnlock(aHandle);
  407. #endif
  408. }
  409.  
  410.  
  411. #ifdef FW_BUILD_MAC
  412. #pragma segment FWMemMgr6
  413. #endif
  414.  
  415. //----------------------------------------------------------------------------------------
  416. // FW_CMemoryManager::GetSystemHandleSize - 
  417. //----------------------------------------------------------------------------------------
  418. unsigned long FW_CMemoryManager::GetSystemHandleSize(FW_PlatformHandle aHandle)
  419. {
  420.     FW_ASSERT(aHandle != 0);
  421.     
  422. #if !defined FW_qUsePlatformAlloc
  423.     return ::MMGetHandleSize(aHandle);
  424. #elif defined FW_BUILD_MAC
  425.     return ::GetHandleSize(aHandle);
  426. #elif defined FW_BUILD_WIN
  427.     return ::GlobalSize(aHandle);
  428. #endif
  429. }
  430.  
  431. //----------------------------------------------------------------------------------------
  432. // FW_CMemoryManager::CopySystemHandle - 
  433. //----------------------------------------------------------------------------------------
  434. FW_PlatformHandle FW_CMemoryManager::CopySystemHandle(FW_PlatformHandle aHandle)
  435. {
  436.     FW_PlatformHandle newHandle = NULL;
  437.     
  438. #if !defined FW_qUsePlatformAlloc
  439.     newHandle = (FW_PlatformHandle) ::MMCopyHandle(aHandle);
  440. #else
  441.     unsigned long size = GetSystemHandleSize(aHandle);
  442.  
  443.     FW_CAcquireLockedSystemHandle sourceLock(aHandle);
  444.     void *sourcePointer = sourceLock.GetPointer();
  445.  
  446.     newHandle = AllocateSystemHandle(size);
  447.     FW_CAcquireLockedSystemHandle destinationLock(newHandle);
  448.     void *destinationPointer = destinationLock.GetPointer();
  449.  
  450.     CopyMemory(sourcePointer, destinationPointer, size);
  451. #endif
  452.  
  453.     return newHandle;
  454. }
  455.  
  456. #ifdef FW_BUILD_MAC
  457. #pragma segment FWMemMgr7
  458. #endif
  459.  
  460. //========================================================================================
  461. // C++ Global operator new & delete
  462. //========================================================================================
  463.  
  464. #ifdef FW_OPERATOR_NEW_SIZE_T_VOIDSTAR
  465. //----------------------------------------------------------------------------------------
  466. // operator new
  467. //----------------------------------------------------------------------------------------
  468.  
  469. void *operator new(size_t /* size */,void *p)
  470. {
  471.     return p;
  472. }
  473. #endif
  474.  
  475. //----------------------------------------------------------------------------------------
  476. // operator new
  477. //----------------------------------------------------------------------------------------
  478. void*       operator new(size_t size)
  479. {
  480.  
  481.     FW_SPrivMemoryGlobals& globals = FW_CMemoryTaskGlobals::GetMemoryGlobals();
  482.     void* p;
  483.     
  484.     globals.gLastRequested = size;
  485.     
  486.     while ((p = FW_PrimitiveAllocateBlock(size)) == 0)
  487.     {
  488.         void  (*newHandler)();
  489.  
  490. #ifdef FW_SET_NEW_HANDLER
  491.         newHandler = globals.gNewHandler;
  492. #else
  493.         // Fetch (and restore) the current handler.  ARM 280-281 uses _new_handler as a
  494.         // global, but I don't know if that's the official definition.
  495.         newHandler = ::set_new_handler(0);
  496.         ::set_new_handler(newHandler);
  497. #endif
  498.         if (newHandler)
  499.             newHandler();
  500.         else
  501.             return 0;
  502.     }
  503.  
  504.     return p;
  505. }
  506.  
  507. //----------------------------------------------------------------------------------------
  508. // operator delete
  509. //----------------------------------------------------------------------------------------
  510. void       operator delete(void *pObject)
  511. {
  512.     FW_CMemoryManager::FreeBlock(pObject);
  513. }
  514.